<?php
namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Exception;

class Qiandao extends Api{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**签到**/
    public function index(){
        $userinfo = $this->auth->getUserinfo();
        $qiandaoModel = new \app\admin\model\qiandao\Qiandao();
        $userModel = new \app\admin\model\User();
        $time0=strtotime(date("Ymd",time()));
        $where['create_time'] = ['>=',$time0];
        $qiandaoinfo = $qiandaoModel->where(['user_id'=>$userinfo['id']])->where($where)->find();
        if($qiandaoinfo){
            $this->error('您今天已经签到过了');
        }else{
            Db::startTrans();
            try{
                $indata['user_id'] = $userinfo['id'];
                $indata['create_time'] = time();
                $qiandaoModel->insert($indata);

                $userModel->where(['id'=>$userinfo['id']])->setInc('hcc_balance',10);
                Db::commit();
                $this->success('签到成功');
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }

    }

    public function list(){
        $userinfo = $this->auth->getUserinfo();
        $page = $this->request->param('page',1);
        $pageSize = 10;
        $start=($page-1)*$pageSize;

        $qiandaoModel = new \app\admin\model\qiandao\Qiandao();
        $list = collection($qiandaoModel->where(['user_id'=>$userinfo['id']])->limit($start,$pageSize)->select())->toArray();
        if($list){
            $this->success('成功',$list);
        }else{
            $this->success('成功',[]);
        }
    }
}